package com.zhengxyou.jetpackdemo.dataBinding;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.Toast;

import com.zhengxyou.jetpackdemo.BR;
import com.zhengxyou.jetpackdemo.R;
import com.zhengxyou.jetpackdemo.databinding.ActivityDataBindingBinding;
import com.zhengxyou.jetpackdemo.databinding.SubLayoutBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.Observable;
import androidx.databinding.ObservableArrayList;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 翻译教程@see https://juejin.im/post/5b02cf8c6fb9a07aa632146d
 * 翻译教程@see https://segmentfault.com/a/1190000002876984#articleHeader0
 * https://blog.csdn.net/qiang_xi/article/details/75379321
 */
public class DataBindingActivity extends AppCompatActivity {
    ActivityDataBindingBinding binding;
    User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_data_binding);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_data_binding);
        ButterKnife.bind(this);

        user = new User("张", "三", 16);
        user.fullName.set("张三");
//        binding.setUser(user);
        binding.setVariable(BR.user, user);
        binding.setIndex(0);
        binding.setList(getList());
        binding.setMap(getMap());
        initListView();

        ObservableArrayList<User> users = new ObservableArrayList<>();
        users.add(new User("1", "2", 3));
//        binding.tvFirstName.setText();

        ObservableChange();

//        binding.setLifecycleOwner(this);
//        binding.view
        binding.setHandlers(new MyHandles());
        binding.setMain(this);

        binding.viewSub.setOnInflateListener(new ViewStub.OnInflateListener() {
            @Override
            public void onInflate(ViewStub stub, View inflated) {
                SubLayoutBinding subLayoutBinding = DataBindingUtil.bind(inflated);
                subLayoutBinding.setViewSub("这是ViewSub");
            }
        });

        user.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                if (propertyId == BR.age) {
                    Log.e("TAG", "BR.age");
                } else if (propertyId == BR.firstName) {
                    Log.e("TAG", "BR.firstName");
                } else if (propertyId == BR.lastName) {
                    Log.e("TAG", "BR.lastName");
                }
            }
        });
    }

    @OnClick(R.id.btn_change_ob)
    void changeOb() {
        Log.e("TAG", binding.getUserAble().firstName.get() + binding.getUserAble().lastName.get() + binding.getUserAble().age.get());
        binding.getUserAble().firstName.set("姓名修改");
        binding.getUser().setHeaderUrl("头像地址");
        binding.getUser().setHeaderId(R.drawable.ic_menu_send);
    }

    private void ObservableChange() {
        UserObserable userObserable = new UserObserable();
        userObserable.age.set(18);
        userObserable.firstName.set("张");
        userObserable.lastName.set("三");

        binding.setUserAble(userObserable);
    }

    private Map<String, String> getMap() {
        Map<String, String> map = new HashMap<>();
        map.put("firstName", "firstName_");
        map.put("first", "first_");
        return map;
    }

    private List<String> getList() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add("list" + i);
        }
        return list;
    }

    private void initListView() {
        RecyclerView recyclerView = findViewById(R.id.rv_dataBind);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        DataBindAdapter adapter = new DataBindAdapter();
        recyclerView.setAdapter(adapter);
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 55; i++) {
            User user = new User("张", i + "", i);
            users.add(user);
        }
        adapter.setData(users);
    }

    @OnClick(R.id.btn_change)
    public void changeName() {
        User user = new User("李", "四", 24);
        user.setDisPlayName("张四");
//        ActivityDataBindingBinding inflate = ActivityDataBindingBindingImpl.inflate(getLayoutInflater());
        binding.setUser(user);
        binding.btnChange.setText(R.string.app_name);
        Log.e("TAG", binding.getUser().getFirstName());

        if (!binding.viewSub.isInflated()) {
            binding.viewSub.getViewStub().inflate();
        }
        user.fullName.set("这是全名修改");
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, DataBindingActivity.class);
        context.startActivity(starter);
    }

    public void onClickLastName(View view) {
        Log.e("TAG", "onClickLastName");
        Toast.makeText(this, "Last Name", Toast.LENGTH_LONG).show();
    }

    public class MyHandles {
        public void onClickFirstName(View view) {
            Log.e("TAG", "ssss");
            user.setFirstName("First Name click");
            Toast.makeText(DataBindingActivity.this, "First Name", Toast.LENGTH_LONG).show();
        }

        public void onClickFirstName(User user) {
            Log.e("TAG", user.getFirstName());

        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.e("TAG", user.getFirstName());
        }
    }
}
